package kr.hkit.coffeemgn.ListUI;

import java.awt.BorderLayout;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;

import kr.hkit.coffeemgn.DTO.CoffeeDTO;

public class CoffeeListUI extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private ArrayList<CoffeeDTO> lists;
	private CustomModel model;
	
	public CoffeeListUI(String title, ArrayList<CoffeeDTO> lists) {
		this.lists = lists;
		initialize(title);
	}
	
	private void initialize(String title) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 700, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JLabel lblTitle = new JLabel(title);
		lblTitle.setFont(new Font("굴림", Font.PLAIN, 18));
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblTitle, BorderLayout.NORTH);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		model = new CustomModel();
		table = new JTable(model);
		scrollPane.setViewportView(table);
	}

	public void refreshData(ArrayList<CoffeeDTO> lists){
		this.lists = lists;
		model.fireTableDataChanged();
	}
	
	class CustomModel extends AbstractTableModel{
		final String[] COLUMN_NAME = {"순위","제품코드","제품명","제품단가","판매수량","공급가액","부가세액","판매금액","마진율", "마진액"};
		final Class[] COLUMN_CLASS = {Integer.class, String.class, String.class,Integer.class,Integer.class,Integer.class,Integer.class,Integer.class,Integer.class,Integer.class};
		@Override
		public int getRowCount() {
			return lists.size();
		}

		@Override
		public Class<?> getColumnClass(int columnIndex) {
			return COLUMN_CLASS[columnIndex];
		}

		@Override
		public String getColumnName(int column) {
			return COLUMN_NAME[column];
		}

		@Override
		public int getColumnCount() {
			return COLUMN_NAME.length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			CoffeeDTO dto = lists.get(rowIndex);
			DecimalFormat df = new DecimalFormat("#,###");
			switch(columnIndex){
			case 0: return dto.getRank()==0?"합계":dto.getRank();
			case 1: return dto.getpCode();
			case 2: return dto.getpName();
			case 3: return dto.getpCost()==0?"":df.format(dto.getpCost());
			case 4: return dto.getpCnt()==0?"":df.format(dto.getpCnt());
			case 5: return dto.calPushCost()==0?df.format(dto.getTotalPush()):df.format(dto.calPushCost());
			case 6: return dto.calAddTax()==0?df.format(dto.getTotalTax()):df.format(dto.calAddTax());
			case 7: return dto.calSaleCost()==0?df.format(dto.getTotalSale()):df.format(dto.calSaleCost());
			case 8: return dto.getpRate()==0?"":df.format(dto.getpRate());
			case 9: return dto.calRateCost()==0?df.format(dto.getTotalRate()):df.format(dto.calRateCost());
			}
			return null;
		}
		
	}
}
